package com.baitiaojun.system.admin.helper;

import com.baitiaojun.common.constants.Constants;
import com.baitiaojun.common.enums.impl.ServerExpEnums;
import com.baitiaojun.common.exception.ServerException;
import com.baitiaojun.redis.core.support.RedisSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CaptchaHelper {

    @Autowired
    private RedisSupport redisSupport;

    /**
     * 验证码校验
     * @param captchaId
     * @param code
     */
    public void validateCaptcha(String captchaId, String code) {
        Object captcha = redisSupport.getValue(Constants.CAPTCHA_CODE_KEY, captchaId);
        if (captcha == null) {
            throw new ServerException(ServerExpEnums.EXP_CAPTCHA_INVALID);
        }
        if (!code.equals(captcha)) {
            throw new ServerException(ServerExpEnums.EXP_CAPTCHA_ERROR);
        }
        redisSupport.delete(Constants.CAPTCHA_CODE_KEY, captchaId);
    }
}
